/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.apache.hop.pipeline.transforms.agentscript;

import org.apache.hop.core.exception.HopException;
import org.apache.hop.core.row.IRowMeta;
import org.apache.hop.core.row.RowDataUtil;
import org.apache.hop.core.util.StringUtil;
import org.apache.hop.pipeline.Pipeline;
import org.apache.hop.pipeline.PipelineMeta;
import org.apache.hop.pipeline.transform.BaseTransform;
import org.apache.hop.pipeline.transform.ITransform;
import org.apache.hop.pipeline.transform.TransformMeta;

/**
 * Executes a JSR-223 compiledScript on the values in the input stream. Selected calculated values
 * can then be put on the output stream.
 *
 * @author Matt Burgess
 */
public class AgentScript extends BaseTransform<AgentScriptMetaData, AgentScriptData>
    implements ITransform {
  private static final Class<?> PKG = AgentScript.class;
  private String inputMessage;
  private String outputMessage;
  private String llmName;

  public AgentScript(
      TransformMeta transformMeta,
      AgentScriptMetaData meta,
      AgentScriptData data,
      int copyNr,
      PipelineMeta pipelineMeta,
      Pipeline pipeline) {
    super(transformMeta, meta, data, copyNr, pipelineMeta, pipeline);
    logBasic("[AgentScript]construct");
  }

  @Override
  public boolean init() {
    if (!super.init()) {
      return false;
    }
    llmName = this.meta.getId();
    inputMessage = this.meta.getInputMessage();
    outputMessage = this.meta.getOutputMessage();
    logBasic("[init]xml input===" + this.meta.getInputMessage());
    logBasic("[init]xml output===" + this.meta.getInputMessage());
    logBasic("[init]xml llmname===" + this.meta.getId());
    IRowMeta inputMeta = getInputRowMeta();
    if (inputMeta == null) logBasic("inputMeta = null ");
    return true;
  }

  /**
   * 设置元数据
   *
   * @param inputMeta 输入元数据
   * @throws HopException
   */
  private void initializeAndLookup(IRowMeta inputMeta) throws HopException {
    first = false;
    logBasic("[initializeAndLookup]#######this.data.outputRowMeta = null ");
    logBasic("[initializeAndLookup]first = false;");
    this.data.outputRowMeta = inputMeta.clone();
    // 增加output字段
    this.meta.getFields(data.outputRowMeta, getTransformName(), null, null, this, metadataProvider);
  }

  @Override
  public boolean processRow() throws HopException {
    logBasic("[processRow]");
    Object[] rowInput = getRow();
    if (rowInput == null) {
      setOutputDone();
      return false;
    }
    IRowMeta inputMeta = getInputRowMeta();
    if (inputMeta == null) {
      logBasic("[script]#######inputMeta = null ");
      return false;
    }
    logBasic("[processRow]#######inputMeta  ok");
    if (first) {
      initializeAndLookup(inputMeta);
    } else {
      logBasic("[processRow]first = true;");
    }
    int inputSize = 0;
    for (Object ob : rowInput) {
      if (ob != null) {
        logBasic("raw rowInput===" + ob);
      }
      inputSize++;
    }
    // 读取inputMessage
    if (!StringUtil.isEmpty(this.inputMessage)) {
      String fieldName = this.inputMessage;
      int argumentIndex = getInputRowMeta().indexOfValue(fieldName);
      logBasic("[processRow] argumentIndex===" + argumentIndex);
    }

    logBasic("[processRow]inputSize= " + inputSize + ";" + inputMeta.size());
    logBasic("[processRow]input===" + this.meta.getInputMessage());
    logBasic("[processRow]output===" + this.meta.getInputMessage());
    logBasic("[processRow]uid===" + this.meta.getId());

    logBasic("[processRow] inputMeta= " + inputMeta.toStringMeta());
    rowInput[0] = "agentscript";
    Object[] output = {"hi", "你好"};
    this.data.outputRowData = RowDataUtil.addRowData(rowInput, inputMeta.size(), output);
    for (Object ob : this.data.outputRowData) {
      if (ob != null) {
        logBasic("[processRow] this.data.outputRowData===" + ob);
      }
    }
    logBasic("[processRow] outputRowMeta.size=" + this.data.outputRowMeta.size());
    putRow(this.data.outputRowMeta, this.data.outputRowData);
    return true;
  }
}
